Ext.ns('ru.proburo.oms.catalog.components');

/**
 * ru.proburo.oms.catalog.components.List
 * @extends ru.proburo.oms.components.grid.AbstractList
 */
ru.proburo.oms.catalog.components.List = Ext.extend(ru.proburo.oms.components.grid.AbstractList, {
	
	// private
	_nodeId:     1,
	
	// private
	_paramNode:  'node',
	
	// private
	_paramQuery: 'search-query',
	
    constructor: function(config)
	{
		config = config || {};
        
		this.store = new ru.proburo.oms.catalog.dao.ItemStore({storeId: 'item-store'});
        
		Ext.applyIf(config, {
			sm: new Ext.grid.CheckboxSelectionModel()
		});
        
        ru.proburo.oms.catalog.components.List.superclass.constructor.call(this, config);
    },
	
	initTBar: function()
	{
		this.searchField 	= new ru.proburo.oms.components.form.SearchField({
			store: 	   this.store,
			width: 	   160,
			paramName: this._paramQuery
		});
		this.categoryFilter = new Ext.form.Checkbox({});
		
		return [
			'Поиск: ',
			' ',
			this.searchField,
			' ',
			this.categoryFilter,
			'в данной категории',
			'->',
			'-'/*,
			{
				iconCls: 'silk-application-view-tile',
				menu:
				[
					{
						text:	 	  'Details',
						//iconCls: 	  'silk-application-view-detail',
						group:   	  'view',
						checkHandler: function(){},
						checked: 	  true
					},
					{
						text:	 	  'List',
						//iconCls: 	  'silk-application-view-list',
						group:   	  'view',
						checkHandler: function(){},
						checked: 	  false
					},
					{
						text:	 	  'Tiles',
						//iconCls: 	  'silk-application-view-tile',
						group:   	  'view',
						checkHandler: function(){},
						checked: 	  false
					}
				]
			}*/
		];
	},
	
	initColumns: function()
	{
		return [
			this.sm,
			{
				id: 	   'sku',
				header:    'Код',
				dataIndex: 'sku',
				width: 	   25,
				renderer:  this._hiliteRenderer,
				scope: 	   this
			},
			{
				id: 	   'name',
				header:    'Наименование',
				dataIndex: 'name',
				width: 	   200,
				renderer:  this._hiliteRenderer,
				scope: 	   this
			},
			{
				id: 	   'vendor',
				header:    'Производитель',
				dataIndex: 'vendor',
				width: 	   70
			},
			{
				id: 	   'article',
				header:    'Артикул',
				dataIndex: 'article',
				width: 	   50
			},
			{
				id: 	   'price',
				header:    'Цена, руб.',
				dataIndex: 'price',
				renderer:  Ext.util.Format.ruMoney,
				width: 	   40
			}
		];
	},
	
	initEvents: function()
	{
		ru.proburo.oms.catalog.components.List.superclass.initEvents.call(this);
		
		this.store.on('beforeload', this._onBeforeLoad, this);
		
		if(this.tree)
		{
			this.tree.on('click', this._onTreeClick, this);
		}
	},
	
	/**
	 * Listeners
	 */
	// private
	_onBeforeLoad: function(store, options)
	{
		var query  = store.baseParams[this._paramQuery];
		
		if(query && false === this.categoryFilter.getValue())
		{
			// TODO: fix 9324
			store.baseParams[this._paramNode] = 9324;
		}
		else
		{
			store.baseParams[this._paramNode] = this.nodeId;
		}
		
		if(Ext.isAir)
		{
			
			var where  = ' WHERE node_id = '+store.baseParams[this._paramNode];
			
			if(query)
			{
				query = query.toLowerCase();
				where += " AND (name LIKE '%"+query+"%' OR sku LIKE '"+query+"%')";
			}
			
			store.setBaseParam('where', where);
		}
		
		return true;
	},	
	// private
	_onTreeClick: function(node, event)
	{
		var title  = node.attributes.name;
		var nodeId = node.attributes.id;
		
		if(typeof(nodeId) != 'undefined' && nodeId != this.nodeId)
		{
			this.nodeId = nodeId;
			
			this.store.load();
			
			if(this.store.baseParams[this._paramQuery] && false === this.categoryFilter.getValue())
			{
				this.searchField.reset();
			}
		}
		
		this.setTitle(title);
		
		var panel = this.activator.panel;
		var key   = panel.items.getKey(this.ownerCt);
		panel.setActiveTab(key);
	},
    
	/**
	 * Renderers
	 */
    // private
	_hiliteRenderer: function(value, metaData, record, rowIndex, colIndex, store)
	{
		if(store && store.baseParams[this._paramQuery])
		{
			var query = store.baseParams[this._paramQuery].toLowerCase();
			var len   = query.length;
			var pos   = value.toLowerCase().indexOf(query);
			
			if(pos !== -1)
			{
				value = value.substr(0, pos) +
						'<b>' + value.substr(pos, len) + '</b>' + 
						value.substr(pos+len);
			}
		}
		
		return value;
	}
});